﻿using System;

namespace YArchitech.MEP
{
	public class HYCross : HYTee
	{
		public double Side2Angle { get; set; }

		public double Side2Coefficient { get; set; }

		public HYTubeSize Side2Size
		{
			get
			{
				return this.m_Side2Size;
			}
			set
			{
				this.m_Side2Size = value;
			}
		}

		public HYTube GetSide2Tube()
		{
			HYComponent hycomponent = base.Side2;
			while (hycomponent != null && !(hycomponent is HYTube))
			{
				hycomponent = hycomponent.Next;
			}
			if (hycomponent == null)
			{
				return null;
			}
			return hycomponent as HYTube;
		}

		public override void UpdateSize()
		{
			base.UpdateSize();
			HYTube side2Tube = this.GetSide2Tube();
			if (side2Tube != null)
			{
				this.m_Side2Size = side2Tube.Size;
			}
		}

		public override void SetCoeffient(double coef, int partIndex)
		{
			if (2 == partIndex)
			{
				base.SideCoefficient = coef;
				return;
			}
			base.SetCoeffient(coef, partIndex);
		}

		public override void CalCoefficient()
		{
			base.CalCoefficient();
			HYTube previousTube = base.GetPreviousTube();
			HYTube hostTube = base.GetHostTube();
			HYTube side2Tube = this.GetSide2Tube();
			this.Side2Coefficient = this.CalSideCoefficient(previousTube, hostTube, side2Tube);
		}

		public override void AttachResistanceToTube()
		{
			base.AttachResistanceToTube();
			HYTube side2Tube = this.GetSide2Tube();
			if (side2Tube != null)
			{
				side2Tube.AddAttachment(this, this.Name, this.Side2Coefficient, 2);
			}
		}

		protected HYTubeSize m_Side2Size;
	}
}
